package com.qifan.energy.widget.dialog;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.qifan.energy.mvp.callback.IEventIndexCallback;
import com.qifan.energy.R;

public class GenderBottomDialog extends DialogFragment {

    private ImageView genderIv;
    /* 默认性别 0未知 1男 ，2女 */
    private int gender = 0;

    private IEventIndexCallback callback = new IEventIndexCallback() {

        @Override
        public void onCancel() {

        }

        @Override
        public void onComfirm(int index) {

        }
    };

    /* 对外提供 */
    public void setEventCallback(IEventIndexCallback callback) {
        this.callback = callback;
    }

    /* 设置标题 */
    public GenderBottomDialog gender(int gender){
        this.gender = gender;
        if (gender == 1){
            genderIv.setImageResource(R.mipmap.icon_gender_male);
        } else if (gender == 2){
            genderIv.setImageResource(R.mipmap.icon_gender_female);
        }
        return this;
    }

    /* 显示 */
    public GenderBottomDialog showNow2(@Nullable FragmentManager manager, @Nullable String tag) {
        showNow(manager, tag);
        return this;
    }

    /*@NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.fragment_dialog_bottom_gender, null);
        Dialog dialog = new Dialog(getActivity(), R.style.DialogFullScreen);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(true);

        //Do something
        findView(view);

        // 设置宽度为屏宽、位置靠近屏幕底部
        Window window = dialog.getWindow();
        window.setBackgroundDrawableResource(R.color.transparent);
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置dialog的位置在底部
        wlp.gravity = Gravity.BOTTOM;
        //设置dialog的动画
        wlp.windowAnimations = R.style.BottomDialogAnimation;
        window.setAttributes(wlp);
        return dialog;
    }*/

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //去掉dialog的标题，需要在setContentView()之前
        this.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = this.getDialog().getWindow();
        //去掉dialog默认的padding
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置dialog的位置在底部
        lp.gravity = Gravity.BOTTOM;
        //设置dialog的动画
        lp.windowAnimations = R.style.BottomDialogAnimation;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());

        final View view = inflater.inflate(R.layout.fragment_dialog_bottom_gender, null);
        findView(view);
        return view;
    }

    /* 设置View */
    private void findView(View view) {

        genderIv = view.findViewById(R.id.gender_iv);
        TextView cancelTv = view.findViewById(R.id.cancel_tv);
        TextView confirmTv = view.findViewById(R.id.confirm_tv);
        genderIv.setOnClickListener(v -> {
            if (gender == 0){// 取反显示
                gender = 2;
                genderIv.setImageResource(R.mipmap.icon_gender_female);
            } else {
                if (gender == 1){// 取反显示
                    gender = 2;
                    genderIv.setImageResource(R.mipmap.icon_gender_female);
                } else if (gender == 2){
                    gender = 1;
                    genderIv.setImageResource(R.mipmap.icon_gender_male);
                }
            }
        });
        cancelTv.setOnClickListener(v -> {
            dismiss();
            if (callback != null) callback.onCancel();
        });
        confirmTv.setOnClickListener(v -> {
            dismiss();
            if (gender == 0){//默认男
                gender = 1;
            }
            if (callback != null) callback.onComfirm(gender);
        });
    }
}
